package com.axjy.service.impl;

import com.axjy.mapper.PermissionMapper;
import com.axjy.mapper.RoleMapper;
import com.axjy.mapper.RolePermissionMapper;
import com.axjy.pojo.Permission;
import com.axjy.pojo.Role;
import com.axjy.pojo.RolePermission;
import com.axjy.service.RoleService;
import com.axjy.vo.TreeNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("roleService")
@Transactional
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void add(Role role) {
        role.setCreationDate(LocalDateTime.now());
        roleMapper.add(role);
    }

    @Override
    public void del(Integer id) {
        roleMapper.del(id);
    }

    @Override
    public void update(Role role) {
        role.setModifyDate(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public List<Role> searchList() {
        return roleMapper.searchList();
    }

    @Override
    public void grant(Integer roleId,Integer[] ids) {
        rolePermissionMapper.delete(Wrappers.<RolePermission>update().eq("role_id",roleId));
        if(ids!=null&&ids.length>0){
            for(Integer id:ids){
                rolePermissionMapper.insert(new RolePermission(roleId,id));
            }
        }
    }

//    private List<Integer> getPermIdList(Integer roleId){
//       // LambdaQueryWrapper<RolePermission> wrapper= Wrappers.lambdaQuery();
//       // wrapper.eq(RolePermission::getRoleId,roleId);
//        //rolePermissionMapper.findPermIdList(wrapper);
//       return rolePermissionMapper.findPermIdList(roleId);
//    }


    private List<TreeNode> searchByParentId(Integer parentId,Integer roleId){
         LambdaQueryWrapper<Permission> wrapper=new LambdaQueryWrapper<>();
          if(parentId==null){
              wrapper.isNull(Permission::getParentId);
          }else {
              wrapper.eq(Permission::getParentId, parentId);
          }
        List<Integer> permIdList=rolePermissionMapper.findPermIdList(roleId);
          List<Permission> list=  permissionMapper.selectList(wrapper);
          return list.stream().map(permission -> {
              TreeNode treeNode=new TreeNode();
              treeNode.setId(permission.getId());
              treeNode.setTitle(permission.getName());
              int count= permissionMapper.selectCount(Wrappers.<Permission>query().eq("parent_id",permission.getId()));
              if(permIdList!=null&&permIdList.contains(permission.getId())&&count==0){
                  treeNode.setChecked(true);
              }
              return treeNode;
          }).collect(Collectors.toList());
    }

    @Override
    public List<TreeNode> searchTreeData(Integer roleId) {
        List<TreeNode> menuList01=this.searchByParentId(null,roleId);
        for (TreeNode node01:menuList01){
            List<TreeNode> menuList02=this.searchByParentId(node01.getId(),roleId);
            node01.setChildren(menuList02);
            for(TreeNode node02:menuList02){
                List<TreeNode> btnList=this.searchByParentId(node02.getId(),roleId);
                node02.setChildren(btnList);
            }
        }
        return menuList01;
    }
}
